<?php
class Habit_reward extends MY_Controller
{	
	function __construct() {
        parent::__construct();
    }

    function get_reward_list(){
    	$data = $this->user_post_data;
    	if(!empty($data)){
            $today_time = strtotime(date("Y-m-d", time()));
            $userid = $data['user_token']['user_id'];
            $sql = "SELECT tsk.name as task_name,tsk.task_time, rew.id,rew.days,rew.reward,rew.add_time,rctts.continuity FROM tiku_habit_reward as rew LEFT JOIN tiku_tasks as tsk on tsk.id=rew.task_id LEFT JOIN tiku_user_children_max_continuity_record AS cmcr ON(cmcr.task_id=rew.task_id AND end_time >= $today_time) LEFT JOIN tiku_user_children_task_type_statistics AS rctts ON(rctts.task_id=cmcr.task_id) WHERE rew.user_id = '{$userid}'";
            $result = $this->db->query($sql)->result_array();
    		$data_obj = array(
                'count' => count($result),
                'list' => $result,
             );
            echo json_encode(array("code" => 1, "data" => $data_obj));   // 
    	}else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    function get_reward_list_by_habit(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $today_time = strtotime(date("Y-m-d", time()));
            $userid = $data['user_token']['user_id'];
            $habit_id = $data['habit_id'];
            $sql = "SELECT tsk.name as task_name,tsk.task_time, rew.id,rew.days,rew.reward,rew.add_time,rctts.continuity FROM tiku_habit_reward as rew LEFT JOIN tiku_tasks as tsk on tsk.id=rew.task_id LEFT JOIN tiku_user_children_max_continuity_record AS cmcr ON(cmcr.task_id=rew.task_id AND end_time >= $today_time) LEFT JOIN tiku_user_children_task_type_statistics AS rctts ON(rctts.task_id=cmcr.task_id) WHERE rew.user_id = '{$userid}' and rew.task_id = $habit_id";
            $result = $this->db->query($sql)->result_array();
            $data_obj = array(
                'count' => count($result),
                'list' => $result,
             );
            echo json_encode(array("code" => 1, "data" => $data_obj));   // 
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }


    function add(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $reward = array(
                'task_id' => $data['task_id'], 
                'user_id' => $data['user_token']['user_id'], 
                'days' => $data['days'], 
                'reward' => $data['reward'],
                'add_time' => time(), 
            );
            $this->db->insert('tiku_habit_reward',$reward);
            
            echo json_encode(array("code" => 1, "data" => null));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    function del(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $reward_id = $data['reward_id'];

            $sql = "delete from tiku_habit_reward where id=$reward_id";
            $this->db->query($sql);
            
            echo json_encode(array("code" => 1, "data" => null));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }


    function get_by_id(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $today_time = strtotime(date("Y-m-d", time()));
            $reward_id = $data['reward_id'];
            $userid = $data['user_token']['user_id'];
            $sql = "SELECT tsk.name as task_name,tsk.task_time, rew.id,rew.days,rew.reward,rew.add_time,rctts.continuity FROM tiku_habit_reward as rew LEFT JOIN tiku_tasks as tsk on tsk.id=rew.task_id LEFT JOIN tiku_user_children_max_continuity_record AS cmcr ON(cmcr.task_id=rew.task_id AND end_time >= $today_time AND cmcr.child_id='{$data['user_token']['user_id']}') LEFT JOIN tiku_user_children_task_type_statistics AS rctts ON(rctts.task_id=cmcr.task_id) WHERE rew.user_id = '{$userid}' and rew.id = $reward_id";
            $result = $this->db->query($sql)->row_array();
            
            echo json_encode(array("code" => 1, "data" => $result));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }


}